package com.gjx.clock.service

import android.app.Service
import android.content.Intent
import android.media.AudioAttributes
import android.media.Ringtone
import android.media.RingtoneManager
import android.net.Uri
import android.os.IBinder
import com.gjx.clock.R



class MusicService() : Service() {
    var uri: Uri? =null
    var ringtone: Ringtone? =null

    override fun onBind(p0: Intent?): IBinder? {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun onCreate() {
        if(uri==null){
            uri= Uri.parse("android.resource://" + packageName + "/" + R.raw.music)
//            uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)

        }
        // 将这个uri设置为闹钟声音类型
        // https://www.cnblogs.com/HDK2016/p/8043247.html
        val uri = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_ALARM)
        ringtone = RingtoneManager.getRingtone(this, uri)
        val aa = AudioAttributes.Builder()
            .setUsage(AudioAttributes.USAGE_ALARM)
            .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
            .build()
        ringtone?.setAudioAttributes(aa)
        ringtone?.isLooping = true
        super.onCreate()
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        //系统声音 https://blog.csdn.net/yinbaoshiguang/article/details/79610775
        ringtone?.play()


        return super.onStartCommand(intent, flags, startId)
    }



    override fun onDestroy() {
        super.onDestroy()
        ringtone?.stop()
    }
}

